#include <string.h>
#include <display_manager.h>
#include <stdio.h>
#include <font_manager.h>
static PDispOpr g_tDispDevs = NULL;
static PDispOpr g_tDispDefault = NULL;
static DispBuffer g_tDispBuff;
static int line_width;
static int pixel_width;
void DrawTextInRegion_Central(char * name, PRegion ptRegion, unsigned int dwColor)
{
	FontBitMap tFontBitMap;
	int iOriginX , iOriginY;
	int error;
	int i = 0;
	RegionCartesian tRegionCartesian;
	Gets_StringRegionCar(name , &tRegionCartesian);
	
	iOriginX = (ptRegion->iWidth - tRegionCartesian.iWidth)/2 + ptRegion->iLeftUpx - tRegionCartesian.iLeftUpx;
	iOriginY = (ptRegion->iHeigh - tRegionCartesian.iHeigh)/2 + ptRegion->iLeftUpY + tRegionCartesian.iLeftUpy;

	while(name[i])
	{
		tFontBitMap.iCurOriginX = iOriginX;
		tFontBitMap.iCurOriginY = iOriginY;
		error = GetFontBitMap(name[i], &tFontBitMap);
		if(error)
		{
			printf("GetFontBitMap error \n");
			return;
		}
		/* 绘制 */
		DrawFontBitMap(&tFontBitMap , dwColor);

		iOriginX = tFontBitMap.iNextOriginX;
		iOriginY = tFontBitMap.iNextOriginY;
		i++;
	}
}


void DrawFontBitMap(PFontBitMap ptFontBitMap , int dwColor)
{
	int  i, j, p, q;
	int  x	   				= ptFontBitMap->tRegion.iLeftUpx;
	int  y	   				= ptFontBitMap->tRegion.iLeftUpY;
    int  x_max 				= x + ptFontBitMap->tRegion.iWidth;
    int  y_max 				= y + ptFontBitMap->tRegion.iHeigh;
	int width  				= ptFontBitMap->tRegion.iWidth;
	unsigned char *buffer 	= ptFontBitMap->pucBuffer;

	
    for ( j = y , q = 0; j < y_max; j++, q++ )
    {
        for ( i = x , p = 0; i < x_max; i++, p++ )
        {
            if ( i < 0 || j < 0 || i >= g_tDispBuff.iXres || j >= g_tDispBuff.iYres )
            	continue;
			if(buffer[q * width + p])
            	putPixel(i, j, dwColor);
        }
    }
}
void DrawRegion(PRegion ptRegion, int dwColor)
{
	int i , j;
	int  x	   				= ptRegion->iLeftUpx;
	int  y	   				= ptRegion->iLeftUpY;
    int  x_max 				= x + ptRegion->iWidth;
    int  y_max 				= y + ptRegion->iHeigh;
	
    for ( j = y ; j < y_max; j++)
    	for ( i = x ; i < x_max; i++)
         	putPixel(i, j, dwColor);

}
int putPixel(int x , int y , int dwColor)
{
	unsigned char *pen_8 = (unsigned char*)(g_tDispBuff.buff +y*line_width+x*pixel_width);
	unsigned short *pen_16;	
	unsigned int *pen_32;	

	unsigned int red, green, blue;	

	pen_16 = (unsigned short *)pen_8;
	pen_32 = (unsigned int *)pen_8;

	switch (g_tDispBuff.iBpp)
	{
		case 8:
		{
			*pen_8 = dwColor;
			break;
		}
		case 16:
		{
			/* 565 */
			red   = (dwColor >> 16) & 0xff;
			green = (dwColor >> 8)  & 0xff;
			blue  = (dwColor >> 0)  & 0xff;
			dwColor = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
			*pen_16 = dwColor;
			break;
		}
		case 32:
		{
			*pen_32 = dwColor;
			break;
		}
		default:
		{
			printf("can't surport %dbpp\n",g_tDispBuff.iBpp);
			return -1;
			break;
		}
	}
	return 0;
}

int FlushDispRegion( PRegion ptRegion , PDispBuffer ptDispBuffer)
{
	return g_tDispDefault->FlushRegion(ptRegion, ptDispBuffer);
}

void RegisterDisplay(PDispOpr ptDispOpr)
{
	ptDispOpr->pNext = g_tDispDevs;
	g_tDispDevs = ptDispOpr;
}

int SelectDefaultDisplay(char * name)
{
	PDispOpr pTmp = g_tDispDevs;
	while(pTmp)
	{
		if(strcmp(pTmp->name , name) == 0)
		{
			g_tDispDefault = pTmp;
			return 0;
		}
		pTmp =  pTmp->pNext;
	}
	return -1;
}
int InitDefaultDisplay(void)
{
	int ret;
	ret = g_tDispDevs->DeviceInit();
	if(ret)
	{
		printf("device init exit\n");
		return -1;
	}
	ret = g_tDispDevs->GetBuffer(&g_tDispBuff);
	if(ret)
	{
		printf("device Get Buffer exit\n");
		return -1;
	}
	line_width = g_tDispBuff.iXres * g_tDispBuff.iBpp /8;
	pixel_width = g_tDispBuff.iBpp /8;

	return 0;
}
PDispBuffer Get_DispBuffer(void)
{
	return &g_tDispBuff;
}


void DisplayInit(void)
{
	extern void frambufferInit(void);
	frambufferInit();
}


